package com.kermel.andromeda.activities.triggered;

import android.app.Activity;
import android.media.Ringtone;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.kermel.andromeda.R;
import com.kermel.andromeda.common.util.BundleUtil;
import com.kermel.andromeda.data.Alarm;
import com.kermel.andromeda.data.AlarmManager;
import com.kermel.common.util.RingtoneUtils;
import com.kermel.common.util.StringUtils;

public class TriggeredAlarmActivity extends Activity {
    private Alarm alarm;
    private TextView triggeredAlarmNameDisplay;
    private TextView triggeredAlarmSoundDisplay;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.triggered);
        
        alarm = BundleUtil.retrieveAlarmFromBundleData(savedInstanceState, getIntent());
        
        playAlarmRingtone();
        
        terminateAlarm();
        
        initializeOnClickListeners();
        
        storeWidgetHandles();
        
        updateWidgetsWithValuesFromAlarm();
    }
    
    private void playAlarmRingtone() {
        if (alarm != null) {
            String ringtoneUriString = alarm.ringtoneUriString;
            
            if (StringUtils.isNotBlank(ringtoneUriString)) {
                Ringtone ringtone = RingtoneUtils.obtainRingtone(this, ringtoneUriString);
                
                if (ringtone != null)
                    ringtone.play();
            }
        }
    }
    
    protected void terminateAlarm() {
        AlarmManager.instance.removeAlarm(alarm);
    }
    
    private void initializeOnClickListeners() {
        View stopButton = findViewById(R.id.triggeredStopAlarmButton);
        stopButton.setOnClickListener(new StopTriggeredAlarmAction(this, alarm));
        
        View snoozeButton = findViewById(R.id.triggeredSnoozeButton);
        snoozeButton.setOnClickListener(new SnoozeTriggeredAlarmAction(this, alarm));
    }
    
    private void storeWidgetHandles() {
        triggeredAlarmNameDisplay = (TextView)findViewById(R.id.triggeredAlarmNameDisplay);
        triggeredAlarmSoundDisplay = (TextView)findViewById(R.id.triggeredAlarmSoundDisplay);
    }

    private void updateWidgetsWithValuesFromAlarm() {
        if (alarm == null) {
            triggeredAlarmNameDisplay.setText(getString(R.string.triggeredDefaultAlarmName));
            triggeredAlarmSoundDisplay.setText(getString(R.string.triggeredDefaultSoundName));
        } else {
            updateAlarmNameDisplay();
            updateAlarmSoundDisplay();
        }
    }    

    private void updateAlarmNameDisplay() {
        if (StringUtils.isBlank(alarm.name))
            triggeredAlarmNameDisplay.setText(getString(R.string.triggeredDefaultAlarmName));
        else 
            triggeredAlarmNameDisplay.setText(alarm.name);
    }
    
    private void updateAlarmSoundDisplay() {
        String ringtoneTitle = alarm.ringtoneTitle;
        if (StringUtils.isBlank(ringtoneTitle))
            triggeredAlarmSoundDisplay.setText(getString(R.string.triggeredDefaultSoundName));
        else
            triggeredAlarmSoundDisplay.setText(ringtoneTitle);
    }
}
